package com.example.wineknower.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.wineknower.R;
import com.example.wineknower.model.Wine;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;


/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class RecycleViewAdapter extends BaseQuickAdapter<Wine, BaseViewHolder> {
    public RecycleViewAdapter(int layoutResId, @Nullable List<Wine> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Wine item) {
        helper.setText(R.id.text,item.getWine_title());
//        returnBitMap(helper, item.getWine_img());
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                try {
                    URL url = new URL(item.getWine_img());
                    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
                    return bitmap;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                helper.setImageBitmap(R.id.icon,bitmap);
            }
        }.execute();
    }

//    public static void returnBitMap(BaseViewHolder helper,final String src){
//        new Thread(){
//
//            @Override
//            public void run() {
//                try {
//                    Log.d("FileUtil", src);
//                    URL url = new URL(src);
//                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                    connection.setDoInput(true);
//                    connection.connect();
//                    InputStream input = connection.getInputStream();
//                    Bitmap myBitmap = BitmapFactory.decodeStream(input);
//                    helper.setImageBitmap(R.id.icon,myBitmap);
////                    return myBitmap;
//                } catch (IOException e) {
//                    // Log exception
////                    return null;
//                }
//            }
//        }.start();
//
//    }
}
